import express from "express";
import multer from "multer";
import {
  ImportService,
  importCollegesInfo,
} from "../../services/school/importService.mjs";

const router = express.Router();
const upload = multer({ dest: "uploads/" });

/**
 * @swagger
 * /api/schools/import:
 *   post:
 *     summary: 导入学校数据
 *     description: 导入学校数据
 *     responses:
 *       200:
 *         description: 数据导入成功
 */
router.post("/", upload.single("file"), async (req, res) => {
  try {
    if (!req.file) {
      return res.status(400).json({ message: "请上传文件" });
    }

    const result = await ImportService.importSchoolData(req.file);
    res.json({
      message: "数据导入成功",
      statistics: result,
    });
  } catch (error) {
    console.error("导入错误:", error);
    res.status(500).json({ message: error.message });
  }
});

/**
 * @swagger
 * /api/schools/import/import-colleges-info:
 *   post:
 *     summary: 导入学校信息数据
 *     description: 导入学校信息数据
 *     responses:
 *       200:
 *         description: 数据导入成功
 */
router.post(
  "/import-colleges-info",
  upload.single("file"),
  async (req, res) => {
    try {
      console.log("上传");
      if (!req.file) {
        return res.status(400).json({ message: "请上传文件" });
      }

      const result = await importCollegesInfo.importCollegesInfo(req.file);
      res.json({ message: "数据导入成功", statistics: result });
    } catch (error) {
      console.error("导入错误:", error);
      res.status(500).json({ message: error.message });
    }
  }
);
export default router;
